#include <bits/stdc++.h>
using namespace std;
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using ll = long long;
ll dx[] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1};
bool px(ll aa,ll bb)
{
    ll a = aa, b = bb;
    ll cnt_a = 0, cnt_b = 0;
    while(a)
    {
        cnt_a += dx[a % 10];
        a /= 10;
    }
    while (b)
    {
        cnt_b += dx[b % 10];
        b /= 10;
    }
    //cout<<a<<":"<<cnt_a<<endl;
    if(cnt_a==cnt_b)
    {
        return aa < bb;
    }
    return cnt_a < cnt_b;
}
void solve()
{
    ll n;
    cin >> n;
    vector<ll> a(n + 1);
    for (ll i = 0;i<n;i++)
    {
        cin >> a[i];
    }

    sort(a.begin(),a.end(),px);

    for (ll i = 1; i <= n;i++)
    {
        cout << a[i] << " ";
    }
}
int main()
{
    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
   return 0;
}
